home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xshp15.zip / XSHARP.C < prev    next >
Text File  |  1992-01-10  |  4KB  |  109 lines

  1. /* 3D animation program to rotate 12 cubes. Uses fixed point.
  2.    All C code tested with Borland C++ 3.0 in C compilation mode
  3.    and the small model. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7. #include <dos.h>
  8. #include "polygon.h"
  9.  
  10. #define FIXED_REPS   0     /* set to # of reps to stop after, or
  11.                               to 0 for free-running */
  12.  
  13. /* Base offset of page to which to draw */
  14. unsigned int CurrentPageBase = 0;
  15.  
  16. /* Clip rectangle; clips to the screen */
  17. int ClipMinX = 0, ClipMinY = 0;
  18. int ClipMaxX = SCREEN_WIDTH, ClipMaxY = SCREEN_HEIGHT;
  19.  
  20. static unsigned int PageStartOffsets[2] =
  21.    {PAGE0_START_OFFSET,PAGE1_START_OFFSET};
  22. int DisplayedPage, NonDisplayedPage;
  23. int RecalcAllXforms = 1, NumObjects;
  24.  
  25. Xform WorldViewXform;   /* initialized from floats */
  26.  
  27. /* Object list start and end */
  28. Object ObjectListStart;
  29. Object ObjectListEnd;
  30.  
  31. void main()
  32. {
  33.    int Done = 0, i;
  34.    Object *ObjectPtr;
  35.    union REGS regset;
  36. #if FIXED_REPS
  37.    int Reps = FIXED_REPS;
  38. #endif
  39.  
  40.    InitializeObjectList();
  41.    InitializeFixedPoint(); /* set up fixed-point data */
  42.    InitializeCubes();      /* set up the cubes and add them to the
  43.                               object list */
  44.    InitializeBalls();      /* ditto for the balls */
  45.    Set320x240Mode(); /* set the screen to mode X */
  46.    ShowPage(PageStartOffsets[DisplayedPage = 0]);
  47.  
  48.    /* Keep transforming the cube, drawing it to the undisplayed page,
  49.       and flipping the page to show it */
  50.    do {
  51.  
  52. #if FIXED_REPS
  53.          if ((--Reps) == 0) Done = 1;
  54. #endif
  55.  
  56.       /* For each object, regenerate viewing info, if necessary */
  57.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  58.             i++, ObjectPtr = ObjectPtr->NextObject) {
  59.          if (ObjectPtr->RecalcXform || RecalcAllXforms) {
  60.             ObjectPtr->RecalcFunc(ObjectPtr);
  61.             ObjectPtr->RecalcXform = 0;
  62.          }
  63.       }
  64.       RecalcAllXforms = 0;
  65.  
  66.       CurrentPageBase =    /* select other page for drawing to */
  67.             PageStartOffsets[NonDisplayedPage = DisplayedPage ^ 1];
  68.  
  69.       /* For each object, clear the portion of the non-displayed page
  70.          that was drawn to last time, then reset the erase extent */
  71.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  72.             i++, ObjectPtr = ObjectPtr->NextObject) {
  73.          FillRectangleX(ObjectPtr->EraseRect[NonDisplayedPage].Left,
  74.             ObjectPtr->EraseRect[NonDisplayedPage].Top,
  75.             ObjectPtr->EraseRect[NonDisplayedPage].Right,
  76.             ObjectPtr->EraseRect[NonDisplayedPage].Bottom,
  77.             CurrentPageBase, 0);
  78.          ObjectPtr->EraseRect[NonDisplayedPage].Left =
  79.               ObjectPtr->EraseRect[NonDisplayedPage].Top = 0x7FFF;
  80.          ObjectPtr->EraseRect[NonDisplayedPage].Right =
  81.                ObjectPtr->EraseRect[NonDisplayedPage].Bottom = 0;
  82.       }
  83.  
  84.       /* Sort the objects so we can draw them back to front */
  85.       SortObjects();
  86.  
  87.       /* Draw all objects */
  88.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  89.             i++, ObjectPtr = ObjectPtr->NextObject)
  90.          ObjectPtr->DrawFunc(ObjectPtr);
  91.  
  92.       /* Flip to display the page into which we just drew */
  93.       ShowPage(PageStartOffsets[DisplayedPage = NonDisplayedPage]);
  94.  
  95.       /* Move and reorient each object */
  96.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  97.             i++, ObjectPtr = ObjectPtr->NextObject)
  98.          ObjectPtr->MoveFunc(ObjectPtr);
  99.  
  100.       /* Check the keyboard */
  101.       if (kbhit())
  102.          if (getch() == 0x1B) Done = 1;   /* Esc to exit */
  103.    } while (!Done);
  104.    /* Return to text mode and exit */
  105.    regset.x.ax = 0x0003;   /* AL = 3 selects 80x25 text mode */
  106.    int86(0x10, ®set, ®set);
  107.    exit(1);
  108. }
  109.